package com.ruoyi.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.IdentityResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.TbCpadUser;
import com.ruoyi.business.service.ITbCpadUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 业务用户Controller
 *
 * @author ruoyi
 * @date 2024-04-29
 */
@RestController
@RequestMapping("/business/user")
public class TbCpadUserController extends BaseController
{
    @Autowired
    private ITbCpadUserService tbCpadUserService;

    /**
     * 查询业务用户列表
     */
    @PreAuthorize("@ss.hasPermi('business:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbCpadUser tbCpadUser)
    {
        startPage();
        List<TbCpadUser> list = tbCpadUserService.selectTbCpadUserList(tbCpadUser);
        return getDataTable(list);
    }

    /**
     * 导出业务用户列表
     */
    @PreAuthorize("@ss.hasPermi('business:user:export')")
    @Log(title = "业务用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbCpadUser tbCpadUser)
    {
        List<TbCpadUser> list = tbCpadUserService.selectTbCpadUserList(tbCpadUser);
        ExcelUtil<TbCpadUser> util = new ExcelUtil<TbCpadUser>(TbCpadUser.class);
        util.exportExcel(response, list, "业务用户数据");
    }

    /**
     * 获取业务用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbCpadUserService.selectTbCpadUserById(id));
    }

    /**
     * 新增业务用户
     */
    @PreAuthorize("@ss.hasPermi('business:user:add')")
    @Log(title = "业务用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbCpadUser tbCpadUser)
    {
        return toAjax(tbCpadUserService.insertTbCpadUser(tbCpadUser));
    }

    /**
     * 修改业务用户
     */
    @PreAuthorize("@ss.hasPermi('business:user:edit')")
    @Log(title = "业务用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbCpadUser tbCpadUser)
    {
        return toAjax(tbCpadUserService.updateTbCpadUser(tbCpadUser));
    }

    /**
     * 删除业务用户
     */
    @PreAuthorize("@ss.hasPermi('business:user:remove')")
    @Log(title = "业务用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbCpadUserService.deleteTbCpadUserByIds(ids));
    }


    /**
     * 检测用户状态
     */
    @GetMapping(value = "/checkUserIdentity/{id}")
    public AjaxResult checkUserIdentity(@PathVariable("id") Long id)
    {
        IdentityResult result = tbCpadUserService.checkUserIdentity(id);
        return success(result);
    }

    /**
     * 导入用户
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TbCpadUser> util = new ExcelUtil<>(TbCpadUser.class);
        List<TbCpadUser> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = tbCpadUserService.importUser(userList, updateSupport, operName, null);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TbCpadUser> util = new ExcelUtil<>(TbCpadUser.class);
        util.importTemplateExcel(response, "用户数据");
    }
}
